//获取应用实例
const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: "",
        type: "", 
        detail: {},
        isShow: false,

        alertShow: false,
        limit: 0,
        number: 1,
        left: false,
        right: true,
        phone: "",
        isShowButton: true,
        isLogin: false,
    },

    // 
    onLoad: function(options) {
        this.setData({ id: options.id, type: options.type });
    },
    onShow: function() {
        this.setData({ isLogin: app.globalData.isLogin })
        this.GetList(); // 请求记录
        this.tesUserTel();
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },
    tesUserTel:   function()  {   
        let that = this;

        // 验证手机号
        wx.request({
            url: app.globalData.Baseurl + 'user/user_tel',
            data: {
                openid: app.globalData.openid || wx.getStorageSync('openid')
            },
            success: function(res) {
                // console.log(app.globalData.tel);
                if (res.data.status == 1) that.setData({ phone: res.data.tel, isShowButton: false });
            }
        }); 
    },
    getPhoneNumber:   function(e)  {  
        let that = this 

        if (e.detail.errMsg == 'getPhoneNumber:ok') { 
            wx.showModal({          
                title:   '提示',
                showCancel:  false,
                content:   '同意授权',
                success:   function (res)  {
                    wx.request({
                        url: app.globalData.Baseurl + 'Login/gettell',
                        data: {
                            encryptedData: e.detail.encryptedData,
                            code: app.globalData.code,
                            iv: e.detail.iv,
                            openid: app.globalData.openid || wx.getStorageSync('openid'),
                            sessionKey: app.globalData.session_key
                        },
                        success: function(res) {
                            app.globalData.tel = res.data.phoneNumber;
                            that.setData({ phone: res.data.phoneNumber, isShowButton: false });
                        }
                    }) 
                }      
            });   
        } else { 
            wx.showModal({          
                title:   '提示',
                showCancel:  false,
                content:   '未授权',
                success:   function (res)  { }      
            });    
        }
    },

    //请求记录
    GetList: function() {
        var that = this;

        wx.request({
            url: app.globalData.Baseurl + 'user/exchange_goods_detail',
            method: 'GET',
            data: {
                id: that.data.id,
                type: that.data.type,
            },
            success: function(res) {
                // console.log(res);
                if (res.data.status == 1) {
                    if(res.data.data.img) res.data.data.img = app.globalData.BaseImgUrl + res.data.data.img;

                    if(res.data.data.mail == 1) res.data.data.mail = '包邮';
                    if(res.data.data.mail == 2) res.data.data.mail = '到付';
                    if(res.data.data.deliver == 1) res.data.data.deliver = '24小时内发货';
                    if(res.data.data.deliver == 2) res.data.data.deliver = '48小时内发货';
                    if(res.data.data.is_vip == 0) res.data.data.is_vip_text = '无限制';
                    if(res.data.data.is_vip == 1) res.data.data.is_vip_text = '会员专享';
                    
                    res.data.data.detail = res.data.data.detail.replace(/\<img/gi, '<img style="width:100%;height:auto" ');

                    that.setData({ detail: res.data.data, limit: res.data.data.limit_num });
                }
            }
        });
    },

    toLog: function(){
        var that = this;
        
        wx.navigateTo({
            url: 'log?id=' + that.data.id + "&type=" + that.data.type,
        })
    },
    left: function(){
        var n = Number(this.data.number);
        if(n > 1){
            this.setData({ number: n -1 });
        }
    },
    right: function(){
        var n = Number(this.data.number), l = Number(this.data.limit);
        if(n < l){
            this.setData({ number: n  + 1 });
        }
    },
    editNum: function(e){
        var l = Number(this.data.limit), v = Number(e.detail.value);
        
        if(v > l) this.setData({ number: l });

        if(v < 1 && e.type == 'blur') this.setData({ number: 1 });
    },
    maxNum: function(){
        var l = this.data.limit;

        this.setData({ number: l });
    },
    alertHide: function(){
        this.setData({ alertShow: false });
    },

    //兑换
    toExchange: function() {
        if(!app.globalData.isLogin){
            wx.navigateTo({
                url: '/pages/Authorized/Authorized',
            });
            return false;
        }
        
        if(this.data.type == 1 || this.data.type == 2){
            this.submit();
        }else{
            this.setData({ alertShow: true });
        }
    },
    submit: function(){
        var that = this, url = '', data = {};

        data.openid = app.globalData.openid || wx.getStorageSync('openid');
        data.id = that.data.id;
        data.is_vip = that.data.detail.is_vip;
        data.tsp = Math.floor(new Date().getTime() / 1000);

        if(that.data.type == 1){
            url += 'user/exchange_coupon';
        }else if(that.data.type == 2){
            url += 'gashapon/exchange_ndb';
        }else{
            url += 'gashapon/exchange_prize';

            data.num = that.data.number;
            data.type = that.data.type;
        }


        data.sign = app.getSign(data);

        wx.request({
            url: app.globalData.Baseurl + url,
            method: 'POST',
            data: data,
            success: function(res) {

                if (res.data.status == 1) {
                    that.setData({ isShow: true, alertShow: false });
                    that.GetList();
                } else {
                    app.showError(res.data.msg);
                }
            }
        });
    },

    popupHide: function() {
        this.setData({ isShow: false });
    },
    toPrizeBox: function() {
        var that = this;
        
        wx.navigateTo({
            url: '../prizeBox/list?id=' + that.data.detail.jump_type,
        });
    },
    toMyNdb: function() {
        var that = this;
        
        wx.navigateTo({
            url: '../person/person',
        });
    },
    


    /**
     * 时间戳转换成 时间  年月日 
     */
    timeFormat: function(time) {
        if(!time || time == 'undefined') return '-';

        var d = new Date(1000 * time);
        var yy = d.getFullYear();
        var mm = d.getMonth() + 1;
        var dd = d.getDate();
        var hh = d.getHours();
        var ff = d.getMinutes();

        if (mm < 10) mm = "0" + mm;
        if (dd < 10) dd = "0" + dd;
        if (hh < 10) hh = "0" + hh;
        if (ff < 10) ff = "0" + ff;

        return yy + "." + mm + "." + dd;
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    // onShareAppMessage: function() {

    // }
})